# -*- coding:UTF-8 -*-
'''
开发环境：PyCharm
创建日期：2025/8/13 16:32
作   者：新启典学苑-樱桃
描   述：操作数据库
'''
import pymysql

from ranzhiAutoTest.util.log import Logger


def connect(host, port, user, pwd, dbname):
    try:
        conn = pymysql.connect(user=user,
            password=pwd,
            host=host,
            database=dbname,
            port=int(port),
            charset="UTF8")
        print(f"连接数据库{host}:{port}成功")
        return conn
    except Exception as e :
        print(f"连接数据库{host}:{port}异常，异常信息为：{e}")

def execute(conn, sql):
    try:
        c = conn.cursor()  # 获取游标
        c.execute(sql)
        conn.commit()
        r = c.fetchall()
        c.close()  # 关闭游标
        Logger().info(f"执行{sql}语句成功")
        return r
    except Exception as e:
        Logger().error(f"执行{sql}语句异常，异常信息为：{e}")

def disconnect(conn):
    conn.close()


def delete_by_user(host, port, user, pwd, dbname, account):
    a = connect(host, port, user, pwd, dbname)
    # sys_user
    execute(a, f"delete from rzsys_user where account='{account}';")
    disconnect(a)

# 测试代码，用完可以删除
if __name__ == '__main__':
    a = connect("192.168.1.29", "3306", "root", "123456", "ranzhi")
    b = execute(a, "select * from rzsys_user;")
    print(b)
    disconnect(a)

